package methodReference;

import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * 方法引用：当要传递给lambda体的操作已经有了实现类，可以使用方法引用(方法引用本质上还是lambda表达式)
 * 使用方法：
 * 1.类::静态方法
 * 2.类::非静态方法
 * 3.对象::非静态方法
 */
public class Demo01 {
    public static void main(String[] args) {
        test01();

        test02();

        test03();

        test04();

        test5();
    }

    //1.对象::非静态方法
    public static void test01() {
        //lambda表达式的写法
        Consumer<String> consumer1 = str -> System.out.println(str);
        consumer1.accept("abc");

        //函数引用的写法
        Consumer<String> consumer2 = System.out::println;
        consumer2.accept("123");
    }

    //2.对象::非静态方法
    public static void test02() {
        User yfq = new User("yfq", 20);
        Supplier<String> supplier = () -> yfq.getName();
        System.out.println(supplier.get());

        Supplier<String> supplier1 = yfq::getName;
        System.out.println(supplier1.get());
    }

    //3.类::静态方法
    public static void test03() {
        Comparator<Integer> comparator = (o1, o2) -> Integer.compare(o1, o2);
        System.out.println(comparator.compare(1, 3));

        Comparator<Integer> comparator1 = Integer::compare;
        System.out.println(comparator1.compare(1, 2));
    }

    //4.类::非静态方法
    public static void test04() {
        Comparator<String> comparator = (o1, o2) -> o1.compareTo(o2);
        System.out.println(comparator.compare("abc", "abd"));

        Comparator<String> comparator1 = String::compareTo;
        System.out.println(comparator1.compare("a", "b"));
    }

    //5.类::非静态方法
    public static void test5() {
        BiPredicate<String, String> biPredicate = (s1, s2) -> s1.equals(s2);
        System.out.println(biPredicate.test("anc", "anc"));

        BiPredicate<String,String> biPredicate1 = String::equals;
        System.out.println(biPredicate1.test("a","a"));
    }

    public static class User {
        public String name;

        public int age;

        public User() {
        }

        public User(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }
}
